package com.xyx.ssm.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xyx.ssm.po.WineProduct;
import com.xyx.ssm.service.WineproductService;
import com.xyx.ssm.util.Page;

@Controller
@RequestMapping(value = "/wineproduct")
public class WineproductAction {
	 @Resource
	private WineproductService wineproductService;

	@RequestMapping("/pageView")
	public String pageView(Model model, Integer pageNow, String pageName,
			String departName) {
//		Map<String, Object> map = new HashMap<String, Object>();
//		// 用户名
//		map.put("pageName", pageName);
//		if (pageNow == null) {
//			pageNow = 1;
//		}
//		System.out.println(map.toString());
//		// 1:当前页码
//		// 1:查询参数
//		Page<Map<String, Object>> page = new Page<Map<String, Object>>(pageNow,
//				map, 2);
//		Page<Map<String, Object>> page1 = wineproductService.findPage(page);
//		page1.setParam(map);
//		model.addAttribute("page", page1);
		return "wine/wineproductlist";
	}

	@RequestMapping("/jsons")
	@ResponseBody
	public Map<String, Object> jsons(Model model, Integer iDisplayLength,
			Integer sEcho, Integer iDisplayStart, String pageName,
			String departName) {

		Map<String, Object> map111 = new HashMap<String, Object>();
		// 用户名
		map111.put("pageName", "");
		if (sEcho == null) {
			sEcho = 1;
		}
		if (iDisplayLength == null) {
			iDisplayLength = 10;
		}
		if (iDisplayStart == null) {
			iDisplayStart = 1;
		}
		System.out.println(map111.toString());
		// 1:当前页码
		// 1:查询参数
		Page<Map<String, Object>> page = new Page<Map<String, Object>>(
				iDisplayStart, map111, iDisplayLength);
		Page<Map<String, Object>> page1 = wineproductService.findPage(page);
		int num = page1.getList().size();
		Map<String, Object> map = new HashMap<String, Object>();
		List<Ceshi> list = new ArrayList<Ceshi>();
		for (int i = 0; i < num; i++) {
			Ceshi ceshi = new Ceshi();
			ceshi.setId(i);
			ceshi.setUsername(page1.getList().get(i).get("id").toString());
			list.add(ceshi);
		}
		map.put("sEcho", sEcho);
		map.put("iTotalRecords", page1.getTotalCount());
		map.put("iTotalDisplayRecords", page1.getTotalCount());
		map.put("aaData", page1.getList());
		return map;
	}

	/**
	 * 酒品牌的添加
	 * 
	 * 
	 */
	@RequestMapping("/wineproductadd")
	@ResponseBody
	public Map<String, Object> wineproductadd(WineProduct wineProduct) {

//		Map<String, Object> map = new HashMap<String, Object>();
//		//
//		map.put("proname", proname);
//		map.put("brandid", brandid);
//		map.put("typeid", typeid);
//		map.put("proaddress", proaddress);
//		map.put("alcohol", alcohol);
//		map.put("winefactoryid", winefactoryid);
//		map.put("promaterial", promaterial);
//		map.put("procapacity", procapacity);
//		map.put("protrait", protrait);
//		map.put("prostandard", prostandard);
//		map.put("prodetail", prodetail);
//		map.put("price", price);
//		map.put("imageurl", imageurl);
//		map.put("imagename", imagename);
		int num = wineproductService.wineproductadd(wineProduct);
		Map<String, Object> mapjson = new HashMap<String, Object>();
		if (num > 0) {
			mapjson.put("flag", true);
			mapjson.put("msg", "添加成功！");
		} else {
			mapjson.put("flag", false);
			mapjson.put("msg", "添加失败！");
		}
		return mapjson;
	}

	@RequestMapping("/wineproductdelete")
	@ResponseBody
	public Map<String, Object> wineproductdelete(String ids) {

		int num = wineproductService.wineproductDelete(ids);
		Map<String, Object> mapjson = new HashMap<String, Object>();
		if (num > 0) {
			mapjson.put("flag", true);
			mapjson.put("msg", "删除成功！");
		} else {
			mapjson.put("flag", false);
			mapjson.put("msg", "删除失败！");

		}
		return mapjson;
	}

	@RequestMapping("/wineproductdetail")
	@ResponseBody
	public Map<String, Object> wineproductdetail(Integer id) {

		System.out.println("so easy -" + id);
		List<WineProduct> list = wineproductService.wineproductId(id);
        System.out.println("-------------------------------:"+list);
		Map<String, Object> mapjson = new HashMap<String, Object>();

		mapjson.put("proname", list.get(0).getProname());
		mapjson.put("brandid", list.get(0).getBrandid());
		mapjson.put("typeid", list.get(0).getTypeid());
		mapjson.put("proaddress", list.get(0).getProaddress());
		mapjson.put("alcohol", list.get(0).getAlcohol());
		mapjson.put("winefactoryid", list.get(0).getWinefactoryid());
		mapjson.put("promaterial", list.get(0).getPromaterial());
		mapjson.put("procapacity", list.get(0).getProcapacity());
		mapjson.put("protrait", list.get(0).getProtrait());
		mapjson.put("prostandard", list.get(0).getProstandard());
		mapjson.put("prodetail", list.get(0).getProdetail());
		mapjson.put("price", list.get(0).getPrice());
		mapjson.put("imageurl", list.get(0).getImageurl());
		mapjson.put("imagename", list.get(0).getImagename());
		mapjson.put("id", id);

		return mapjson;
	}
	
	@RequestMapping("/wineproductupdate")
	@ResponseBody
	public Map<String, Object> wineproductUpdate(WineProduct wineProduct) {

//		WineProduct wineProduct =new WineProduct();
//		
//		wineProduct.setId(Integer.parseInt(id));
//		wineProduct.setProname(proname);
//		wineProduct.setBrandid(brandid);
//		wineProduct.setTypeid(typeid);
//		wineProduct.setProaddress(proaddress);
//		wineProduct.setAlcohol(alcohol);
//		wineProduct.setWinefactoryid(winefactoryid);
//		wineProduct.setPromaterial(promaterial);
//		wineProduct.setProcapacity(procapacity);
//		wineProduct.setProtrait(protrait);
//		wineProduct.setProstandard(prostandard);
//		wineProduct.setProdetail(prodetail);
//		wineProduct.setPrice(price);
//		wineProduct.setImageurl(imageurl);
//		wineProduct.setImagename(imagename);
		int num= wineproductService.wineproductUpdate(wineProduct);
		Map<String, Object> mapjson = new HashMap<String, Object>();
		if(num>0){
			mapjson.put("flag", true);
			mapjson.put("msg", "修改成功！");
		}else{
			mapjson.put("flag", false);
			mapjson.put("msg", "修改失败！");
		}

		return mapjson;
	}
	
}